package com.zhiwen.controller;

import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.LoginDTO;
import com.zhiwen.domain.dto.RegisterDTO;
import com.zhiwen.service.AuthService;
import com.zhiwen.domain.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 认证控制器
 */
@Tag(name = "认证管理", description = "用户认证与登录相关接口")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "通过用户名和密码登录系统")
    @ApiResponse(responseCode = "200", description = "登录成功，返回token信息")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody @Valid LoginDTO loginDTO) {
        Map<String, Object> tokenInfo = authService.login(loginDTO);
        return Result.success(tokenInfo);
    }

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册", description = "新用户注册")
    @ApiResponse(responseCode = "200", description = "注册成功，返回用户信息",
                content = @Content(schema = @Schema(implementation = UserVO.class)))
    @PostMapping("/register")
    public Result<UserVO> register(@RequestBody @Valid RegisterDTO registerDTO) {
        UserVO userVO = authService.register(registerDTO);
        return Result.success(userVO);
    }

    /**
     * 获取邮箱验证码
     */
    @Operation(summary = "获取邮箱验证码", description = "发送验证码到指定邮箱")
    @ApiResponse(responseCode = "200", description = "发送成功")
    @PostMapping("/captcha")
    public Result<Map<String, String>> getCaptcha(
            @Parameter(description = "包含email字段的参数")
            @RequestBody Map<String, String> params) {
        String email = params.get("email");
        Map<String, String> captchaInfo = authService.generateCaptcha(email);
        return Result.success(captchaInfo);
    }

    /**
     * 获取当前登录用户信息
     */
    @Operation(summary = "获取当前登录用户信息", description = "获取当前登录用户的详细信息")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "成功",
                content = @Content(schema = @Schema(implementation = UserVO.class)))
    @GetMapping("/user")
    public Result<UserVO> getCurrentUser() {
        UserVO userVO = authService.getCurrentUser();
        return Result.success(userVO);
    }

    /**
     * 用户登出
     */
    @Operation(summary = "用户登出", description = "退出登录状态")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "登出成功")
    @PostMapping("/logout")
    public Result<Object> logout() {
        authService.logout();
        return Result.success();
    }
}
